package com.myFwk.service.serviceImpl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.myFwk.constant.Constants;
import com.myFwk.service.MessageSendService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


/**
 * @Title:
 * @Description:
 * @Author: guowl
 * @version： 1.0
 * @Date:2021/10/20
 * @Copyright: Copyright(c)2021 RedaFlight.com All Rights Reserved
 */
@Service
@Slf4j
public class MessageSendServiceImpl implements MessageSendService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ConfirmCallbackService confirmCallbackService;
    @Autowired
    private ReturnCallBackService returnCallBackService;

    @Override
    public void sendMessage(String content) {
        setRabbitTemplate(rabbitTemplate);
        String s = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_FWK, Constants.ROUTING_KEY_0, content, message -> {
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            return message;
        }, new CorrelationData(UUID.randomUUID().toString()));
    }

    private void setRabbitTemplate(RabbitTemplate template) {
        /**
         * 设置开启confirmCallback
         */
        rabbitTemplate.setExchange(Constants.EXCHANGE_FWK);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(confirmCallbackService);
        rabbitTemplate.setReturnCallback(returnCallBackService);
    }


    public static void main(String[] args) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long("1631671496000");
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        System.out.println(res);
    }
}
